package training;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_7_14_Training {
    //进行操作使字符串为空
    public String lastNonEmptyString(String s) {
        int[] d = new int[26];
        int max = 0;
        for (char c : s.toCharArray()) {
            d[c - 'a']++;
            max = Math.max(max, d[c - 'a']);
        }
        StringBuilder sb = new StringBuilder();
        int[] cnt = new int[26];
        for (char c : s.toCharArray()) {
            cnt[c - 'a']++;
            if (cnt[c - 'a'] == max) {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    //统计道路上的碰撞次数
    public int countCollisions(String directions) {
        char [] arr = directions.toCharArray();

        int left = 0, right  = arr.length - 1;
        while(left < arr.length && arr[left] == 'L'){
            left++;
        }
        while(right >= 0 && arr[right] == 'R'){
            right--;
        }

        int  ans = 0;
        while(left <= right){
            if(arr[left] != 'S'){
                ans++;
            }
            left++;
        }

        return ans;
    }
}
